package cn.wangqi.personal.controller;

import cn.wangqi.personal.common.BaseController;
import cn.wangqi.personal.domain.Major;
import cn.wangqi.personal.service.IMajorService;
import cn.wangqi.personal.util.Page;
import cn.wangqi.personal.util.PageResultModel;
import cn.wangqi.personal.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * Created by admin on 2017/9/12.
 */
@Controller
@RequestMapping("major")
public class MajorController extends BaseController{

    Logger logger = Logger.getLogger(CollageController.class);

    @Resource
    public IMajorService majorService;
    @RequestMapping("index.html")
    public String index(Model model){
        model.addAttribute("code","major");
        return "major/index";
    }

    @RequestMapping("findMajorList.html")
    @ResponseBody
    public String findMajorList(Integer currentPage,Integer collageId){
        Page page = null;
        if(currentPage != null && currentPage >= 1){
            page = new Page();
            page.setPageSize(10);
            page.setStart((currentPage - 1) * 10);
        }
        List<Major> majorList = majorService.listMajor(collageId,page);
        int count = majorService.countMajor(collageId);
        PageResultModel<Major> result = new PageResultModel();
        result.setPageNo(currentPage!=null?currentPage:1);
        result.setPageSize(page != null ?page.getPageSize():1);
        result.setTotal(count);
        result.setDataList(majorList);
        return buildSuccessResponse((Serializable) result);
    }

    @RequestMapping("saveMajor.html")
    @ResponseBody
    public String saveMajor(Major major){
        try{
            if(major==null){
                return this.buildErrorResponse("请输入信息");
            }
            if(major.getCollageId()==null){
                return this.buildErrorResponse("请选择学院");
            }
            if(major.getMajorId()==null){
                return this.buildErrorResponse("请输入专业编号");
            }
            if(StringUtil.isEmpty(major.getMajorName())){
                return this.buildErrorResponse("请输入专业名称");
            }
            if(major.getYears()==null){
                return this.buildErrorResponse("请选择学制");
            }
            if(major.getDegree()==null){
                return this.buildErrorResponse("请选择学位");
            }
            majorService.saveMajor(major);
            return buildSuccessResponse();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return this.buildErrorResponse(e.getMessage());
        }
    }

    @RequestMapping("deleteMajorById.html")
    @ResponseBody
    public String deleteMajorById(Integer majorId){
        try{
            if(majorId==null||majorId==0){
                return this.buildErrorResponse("删除失败");
            }
            majorService.deleteMajorById(majorId);
            return buildSuccessResponse();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return this.buildErrorResponse(e.getMessage());
        }
    }
    @RequestMapping("findMajorById.html")
    @ResponseBody
    public String findMajorById(Integer majorId){
        try{
            if(majorId==null||majorId==0){
                return this.buildErrorResponse("id为空");
            }
            Major major = majorService.findMajorById(majorId);
            return buildSuccessResponse(major);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return this.buildErrorResponse(e.getMessage());
        }
    }
    @RequestMapping("updateMajor.html")
    @ResponseBody
    public String updateMajor(Major major){
        try{
            if(major==null){
                return this.buildErrorResponse("请输入信息");
            }
            if(major.getCollageId()==null){
                return this.buildErrorResponse("请选择学院");
            }
            if(major.getMajorId()==null){
                return this.buildErrorResponse("请输入专业编号");
            }
            if(StringUtil.isEmpty(major.getMajorName())){
                return this.buildErrorResponse("请输入专业名称");
            }
            if(major.getYears()==null){
                return this.buildErrorResponse("请选择学制");
            }
            if(major.getDegree()==null){
                return this.buildErrorResponse("请选择学位");
            }
            majorService.updateMajor(major);
            return buildSuccessResponse();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return this.buildErrorResponse(e.getMessage());
        }
    }
}
